package com.smm.imgServer.bo.impl;

import com.smm.imgServer.bo.ImageInfoBo;
import com.smm.imgServer.dao.ImageInfoDao;
import com.smm.imgServer.domain.ImageInfo;
import com.smm.imgServer.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * Created by tangshulei on 2016/2/1.
 */
@Service("imageInfoBo")
public class ImageInfoBoImpl implements ImageInfoBo{

    @Value("${upload.path}")
    private String path;

    @Autowired
    private ImageInfoDao imageInfoDaoImpl;

    public String upload(MultipartFile file,String clientId){
        String fileName = file.getOriginalFilename();
        if(fileName == null){
            return "文件名为空";
        }
        if(file.getSize() > 10000000){
            return "文件大小不能超过10M";
        }

        String fileType = fileName.substring(fileName.lastIndexOf("."));

        /**
         * 第一层拦截：直接获取文件名字判断是否是图片文件
         */
        if (!fileType.contains("jpg") && !fileType.contains("bmp") && !fileType.contains("jpeg")
                && !fileType.contains("gif") && !fileType.contains("tiff") && !fileType.contains("psd")
                && !fileType.contains("tga") && !fileType.contains("png") && !fileType.contains("eps")) {
//            rtnMap.put("info", "请确认上传的文件是图片格式");
            return "请确认上传的文件是图片格式";
        }

        /**
         * 第二层拦截:用文件头判断图片格式
         */

        byte[] bt = new byte[4];
        try {
            bt= Arrays.copyOf(file.getBytes(), 4);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String str = FileUtil.getPicType(bt);
        if(str!=null){
            if(str.contains("noimg")){
                //rtnMap.put("info", "只支持以下图片格式:jpg、jpeg、png、gif、bmp");
                return "只支持以下图片格式:jpg、jpeg、png、gif、bmp";
            }
        }else{
//            rtnMap.put("info", "请确认上传的文件是图片格式");
            return "请确认上传的文件是图片格式";
        }


        String newName = FileUtil.getMuFileMD5(file) + fileType;
        String first = newName.substring(0,1);
        String second = newName.substring(1,2);
        String newPath = path + "/" + first + "/" + second;
        File newFile = new File(newPath,newName);
//        File targetFile = new File(path, fileName);
        if(!newFile.exists()){
            newFile.mkdirs();
        }else{
            ImageInfo imageInfo = new ImageInfo();
            imageInfo.setImageName(newName);
            imageInfo.setOrgName(fileName);
            imageInfo.setClientId(clientId);
            ImageInfo imageInfo1 = imageInfoDaoImpl.queryOne(imageInfo);
            if(imageInfo1 == null) {
                imageInfoDaoImpl.save(imageInfo);
            }
            return newName;
        }

        //保存文件
        try {
            file.transferTo(newFile);
        } catch (Exception e) {
            e.printStackTrace();
            return "保存文件出错";
        }

        ImageInfo imageInfo = new ImageInfo();
        imageInfo.setOrgName(fileName);
        imageInfo.setImageName(newName);
        imageInfo.setClientId(clientId);
        ImageInfo imageInfo1 = imageInfoDaoImpl.queryOne(imageInfo);
        if(imageInfo1 == null) {
            imageInfoDaoImpl.save(imageInfo);
        }

        return newName;
    }
}
